package com.testMain.filetest;

import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * @author Jiamingzhen
 * @Date 2021/4/29 10:12
 * @ClassName Test3
 * @Introduce
 */
public class Test3 {
    public static void main(String[] args) throws IOException {
        mkdirsUtil("E:/临时文件夹/临时文件夹的临时文件夹/测试文件夹","测试text.txt","E:/重生柯南当侦探.txt");

        FileOutputStream fout = new FileOutputStream("E:/临时压缩.zip");
        ZipOutputStream zos = new ZipOutputStream(fout);
        ZipOutputStream s= zipDir("E:/临时文件夹",zos,  "测试压缩");
        s.close();
        fout.close();
        zos.close();

    }

    public static void  mkdirsUtil(String filePath,String sourceName,String oldFile) throws IOException {
        File file = new File(filePath);
        File parent =file.getParentFile();
        File source = new File(filePath,sourceName);
        if(parent!=null){
            parent.mkdirs();
        }
        if(!file.exists()){
            file.mkdirs();
        }
        if(!source.exists()){
            source.createNewFile();
        }
        byte[] buffer = new byte[1024];
        int len = 0;

        File old = new File(oldFile);
        FileInputStream fileInputStream = new FileInputStream(old);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        FileOutputStream fileOutputStream = new FileOutputStream(source);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        while ((len=bufferedInputStream.read(buffer))>0){
            bufferedOutputStream.write(buffer,0,len);
        }

        bufferedInputStream.close();
        bufferedOutputStream.close();
        fileInputStream.close();
        fileOutputStream.close();

    }


    /**
     *
     * @param directoryName 读取需压缩文件目录
     * @param zos 创建.zip文件
     * @param basePath 压缩包中的文件名
     * @return
     */
    public static ZipOutputStream zipDir(String directoryName, ZipOutputStream zos, String basePath) {
        File file = new File(directoryName);
        // 每一级别的递归 basePath 不应该被改变所以添加一个 参数 copyBasePath
        String copyBasePath ="";
        if (file.exists()) {
            File[] fileList = file.listFiles();
            for (File f : fileList) {
                if (f.isDirectory()) {
                    // 拼接文件夹目录
                    if (!"".equals(basePath)) {
                        copyBasePath = basePath+File.separator+f.getName();
                    } else {
                        copyBasePath = f.getName();
                    }
                    // 继续递归文件夹
                    zipDir(directoryName + File.separator + f.getName(), zos, copyBasePath);
                } else {
                    // 压缩单个文件到 zos
                    String zipName;
                    if (!"".equals(basePath)) {
                        zipName = basePath + File.separator + f.getName();
                    } else {
                        zipName = f.getName();
                    }
                    try {
                        // zos.putNextEntry  开始添加压缩文件  ZipEntry传入的参数 zipName如果包含了层级关系就会生成文件夹
                        zos.putNextEntry(new ZipEntry(zipName));
                        int len =0;
                        FileInputStream is = new FileInputStream(f);
                        BufferedInputStream bInStream = new BufferedInputStream(is);
                        byte[] bytes = new byte[1024];
                        while ((len = bInStream.read(bytes)) != -1) {
                            zos.write(bytes, 0, bytes.length);
                        }
                        zos.flush();
                        // zos.closeEntry()   结束当前压缩文件的添加
                        zos.closeEntry();
                        is.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
//                Boolean isDelete = f.delete();
//                System.out.println(f.getName()+"删除是否成功: "+isDelete);
            }


        }
        return zos;
    }
}
